<?php
/*------------------------------------------------------------------------------
this file is part of Web Application Kit http://webappkit.net
Copyright (C) 2006 J.Ducastel <jducastel@jducastel.fr>

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
------------------------------------------------------------------------------*/

/**
* trigger storage and access service
* @author J.Ducastel <jducastel@jducastel.fr>
* @version 
*/
class wakTriggerStorage extends wakService {
/*------------------------------------------------------------------------------
                                                                     constructor
------------------------------------------------------------------------------*/
	/**
	*
	* @access
	*/
	function wakTriggerStorage() {
		$this->__construct();
	}/**/
	
	/**
	* "real" constructor
	* @access
	*/
	function __construct() {
		parent::__construct();
		$lib=&wak::getservice('waklib');
		$lib->needclass('waktrigger');
		$lib->needclass('wakparameters');
	}/**/
/*------------------------------------------------------------------------------
                                                                  public methods
------------------------------------------------------------------------------*/
	/**
	* builds request-applying triggers pile from stores
	* @access public
	* @param httprequestbean &$req
	* @return wakTriggersPile
	*/
	function & buildRequestTriggersPile(&$req) {
		// building pile instance
		wakapi::needclass('waktriggerspile',true);
		$pile=&new waktriggerspile();
		// getting core triggers
		//$this->_buildPileFromIniFiles($req,WAK_CORE_PATH.'triggers.store');
		$coreNode=&new foldernode(WAK_CORE_PATH.'triggers.store');
		$this->_buildPileFromFolderNode($req,$coreNode,$pile);
		// getting app triggers
		//$this->_buildPileFromIniFiles($req,'triggers.store');
		$appNode=&new foldernode('app/triggers');
		$this->_buildPileFromFolderNode($req,$appNode,$pile);
		//$pile=&wak::getservice('waktriggerspile');
		//echo wakdebug::vardump($pile);
		return $pile;
	}/**/
	
	/**
	* 
	* @param
	* @access public
	* @return 
	* /
	function () {
		
	}/**/
/*------------------------------------------------------------------------------
                                                                      properties
------------------------------------------------------------------------------*/
	/**
	* @var type desc
	*/
	//var //
/*------------------------------------------------------------------------------
                                                                 private methods
------------------------------------------------------------------------------*/
	
	/**
	* build triggers pile from yaml files
	* @access protected
	* @param httprequestbean &$req
	* @param foldernode &$node
	* @param waktriggerspile &$pile
	* @return bool
	*/
	function _buildPileFromFolderNode(&$req,&$node,&$pile) {
		if (!$node->exists())
			trigger_error("missing folder ".$node->path,E_USER_WARNING);
		// getting yaml nodes
		// building regex
		$regex='^('.$req->getUriSegment(0).'\.)?([1-7]\.)?waktriggers\.yml$';
		$nodes=$node->regexSearchName($regex);
		//echo wakdebug::vardump($nodes);
		for ($n=0; $n<count($nodes); $n++) {
			$yamlNode=&$nodes[$n];
			// $this->_IniFileNodeTriggersToPile($node,$req);
			$this->_yamlFileNodeTriggersToPile($yamlNode,$req,$pile);
		}
		return true;
	}


	/**
	* 
	* @param
	* @access private
	* @return array
	* /
	function _getRequestIniFileNodesFromFolder(&$req,$path) {
		$result=array();
		$node=pathnode::getnode($path);
		if (!is_a($node,'foldernode'))
			return $result;
		// building regex
		$regex='^('.$req->getUriSegment(0).'\.)?([1-7]\.)?waktriggers\.ini$';
		return $node->regexSearchName($regex);
	}/**/
	
	/**
	* import request triggers from yaml filenode into pile
	* @access private
	* @param yamlfilenode &$node
	* @param httprequestbean &$req
	* @param waktriggers pile &$pile
	* @return bool
	*/
	function _yamlFileNodeTriggersToPile(&$node,&$req,&$pile) {
		//$pile=&wak::getservice('waktriggerspile');
		//echo "<p>dumping triggers from ".$ifNode->getpath()."</p>";
		// reading content
		$node->load();
		$list=$node->getData();
		$deb=&wak::getservice('wakdebug');
		//echo $deb->vardump($list);
		//return true;
		// building and gathering triggers
		//$basket=array();
		foreach ($list as $id => $data) {
			$data['id']=$id;
			$data['parameters']=isset($data['parameters'])?$data['parameters']:null;
			$data['parameters']=&new wakparameters($data['parameters']);
			$trigger=&new wakTrigger($data); // & IS important
			//echo $deb->vardump($trigger);
			if ($trigger->matchRequest($req)) {
				//echo $trigger->geteventCall().' is matching request ';
				//echo $deb->vardump($trigger);
				$pile->addTrigger($trigger);
				//echo $deb->vardump($trigger);
			}
		}
		return true;
	}/**/
	
	/**
	* 
	* @param
	* @access private
	* @return 
	* /
	function () {
		
	}/**/
}
?>
